package org.eae.eldf.webapp.controller.base;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.dto.base.BaseLanguageDto;
import org.eae.eldf.entity.base.BaseLanguage;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.base.BaseLanguageService;
import org.eae.eldf.service.module.ModuleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

@Controller
public class BaseLaguageController extends AbstractController<BaseLanguageDto> {
	private BaseLanguageService baseLanguageService;
	private ModuleInfoService moduleInfoService;

	@Autowired
	public void setBaseLanguageService(BaseLanguageService baseLanguageService) {
		this.baseLanguageService = baseLanguageService;
	}

	@Autowired
	public void setModuleInfoService(ModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}
	
	/**
	 * 查询
	 * @param modelMap
	 * @param request
	 * @param searchValue
	 * @param searchValueInfo
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/base/language/list")
	public String initPage(ModelMap modelMap, HttpServletRequest request,
			@RequestParam(required = false) String searchValue,
			@RequestParam(required = false) String searchValueInfo,
			HttpServletResponse response) {

		// 查询moduleInfo 的名称
		//List<ModuleInfo> moduleInfoList = moduleInfoService.queryList(Criteria.create(ModuleInfo.class).where("moduleIsDelete", 0));
		List<ModuleInfo> moduleInfoList = moduleInfoService.queryList(Criteria.create(ModuleInfo.class).where("moduleIsDelete", new Object[]{0}));
		modelMap.addAttribute("listModuleInfos", moduleInfoList); // 模块信息Id

		Pagination<BaseLanguageDto> pager = this.initPager(request, response);
		pager = baseLanguageService.pageLanguageList(pager, searchValue, searchValueInfo);

		modelMap.addAttribute("list", pager);
		modelMap.addAttribute("searchValue", searchValue);

		modelMap.addAttribute("searchValueInfo", searchValueInfo);

		return "/base/language/list";
	}
	
	/**
	 * 页面跳转
	 * @param id
	 * @param modelMap
	 * @param moduleInfoId
	 * @param baseLanguageId
	 * @param searchValue
	 * @return
	 */
	@RequestMapping("/base/language/form")
	@Token(save=true)
	public String toRoleFormPage(@RequestParam(required=false) Long id,
			ModelMap modelMap,
			@RequestParam(required=false) Integer moduleInfoId,
			@RequestParam(required=false) Integer baseLanguageId,
			@RequestParam(required=false) String searchValue){
			
		BaseLanguage baseLanguage= null;
		if(null!=id){
			baseLanguage=baseLanguageService.findById(id);
		}
		List<ModuleInfo> moduleInfoList = moduleInfoService.queryList(Criteria.create(ModuleInfo.class).where("moduleIsDelete", new Object[]{0}));
    	modelMap.addAttribute("listModuleInfos",moduleInfoList); //模块信息Id
    	
    	modelMap.addAttribute("baseLanguage",baseLanguage);
    	modelMap.addAttribute("searchValue",searchValue);
    	
		return "/base/language/form";
		
	}
	
	/**
	 * 新增操作
	 * @param modelAndView
	 * @param moduleInfoId
	 * @param lKey
	 * @param enVal
	 * @param cnVal
	 * @param extValOne
	 * @param extValTwo
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/base/language/add")//判断base/language 的base_language_id 是否存在，存在为add 标签下的add 
	@Token(remove=true,repeatSubmitForwardURL="/base/language/form") //获取路径
	public ModelAndView insert(ModelAndView modelAndView,
			@RequestParam(required=false) Long moduleInfoId,
    		@RequestParam(required=false) String lKey,
    		@RequestParam(required=false) String enVal,
    		@RequestParam(required=false) String cnVal,
    		@RequestParam(required=false) String extValOne,
    		@RequestParam(required=false) String extValTwo,    		
    		HttpServletRequest request,ModelMap modelMap){
		modelAndView.setViewName("forward:/base/language/form");
		//查询lkey是否重复 若重复 给出提示 "角色名已经存在"
			if (baseLanguageService.queryCount(Criteria.create(BaseLanguage.class).where("l_key", new Object[]{lKey}))>0) {
				RequestContext context=new RequestContext(request);
				modelMap.addAttribute("errorMsg",context.getMessage("当前lkey已经存在!"));
				modelAndView.addObject("success",false);
			}
		else {
			BaseLanguage baseLanguage = new BaseLanguage();
			 baseLanguage.setlKey(lKey);
			 baseLanguage.setCnVal(cnVal);
			 baseLanguage.setEnVal(enVal);
			 baseLanguage.setExtValOne(extValOne);
			 baseLanguage.setExtValTwo(extValTwo);
		     baseLanguage.setCreateTime(new Date());
		     baseLanguage.setCreateUser(SpringSecurityUtils.getCurrentUserName());
		     baseLanguage.setLastUpdateTime(new Date());
		     baseLanguage.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());
		     baseLanguage.setModuleInfoId(moduleInfoId);
		     
		     baseLanguageService.insert(baseLanguage);
		     modelAndView.addObject("success",true);
		     
		}
				return modelAndView;
	}
	
	/**
	 * 编辑操作
	 * @param id
	 * @param modelAndView
	 * @param modelMap
	 * @param request
	 * @param searchValue
	 * @param moduleInfoId
	 * @param lKey
	 * @param enVal
	 * @param cnVal
	 * @param extValOne
	 * @param extValTwo
	 * @param lastUpdateTime
	 * @param lastUpdateUser
	 * @return
	 */
	@RequestMapping("/base/language/edit")
	public ModelAndView update(@RequestParam(required=false)Long id,
			ModelAndView modelAndView,
			ModelMap modelMap,HttpServletRequest request,
    		@RequestParam(required=false)String lKey,
    		@RequestParam(required=false)String enVal,
    		@RequestParam(required=false)String cnVal,
    		@RequestParam(required=false)String extValOne,
    		@RequestParam(required=false)String extValTwo,
    		@RequestParam(required=false)Date lastUpdateTime,
    		@RequestParam(required=false)String lastUpdateUser,
    		@RequestParam(required=false)String searchValue ){
			modelAndView.setViewName("forward:/base/language/form");
			baseLanguageService.update(Criteria.create(BaseLanguage.class)				
				.set("lKey", lKey)
				.set("enVal", enVal)
				.set("cnVal", cnVal)
				.set("extValOne", extValOne)
				.set("extValTwo", extValTwo)
				.set("lastUpdateTime", new Date())
				.set("lastUpdateUser", SpringSecurityUtils.getCurrentUserName())
				.where("baseLanguageId",new Object[]{id}));
		modelAndView.addObject("success",true);
    	return modelAndView;
		
	}
			
	/**
	 * 删除语言
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/base/language/del")
	public @ResponseBody
	String delete(@RequestParam Long id) {
		try {
			baseLanguageService.deleteByLaguageId(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";

	}
}
